{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0073d9ac-e4ed-4298-afdf-23aa3bff7c9e",
   "metadata": {},
   "source": [
    "# for 循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf51c2ac-f840-4396-ac27-544198de3524",
   "metadata": {},
   "source": [
    "```python\n",
    "for <variable> in <sequence>:\n",
    "    <statements>\n",
    "else:\n",
    "    <statements>\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebe24f51-abdd-43db-85cd-1cdd212289f1",
   "metadata": {},
   "source": [
    ">for item in iterable:\n",
    ">      \\# 循环主体\n",
    "> else:\n",
    ">       \\# 循环结束后执行的代码"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7397daf-0e40-41ea-a23c-c0cc4500032b",
   "metadata": {
    "tags": []
   },
   "source": [
    "![img](https://www.runoob.com/wp-content/uploads/2013/11/A71EC47E-BC53-4923-8F88-B027937EE2FF.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f4f43957-2c07-4452-95dd-0194fb709286",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "sites = [\"Baidu\", \"Google\",\"JD\",\"Taobao\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "feedb8ab-56a3-4341-a36f-304a1b82385a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Baidu\n",
      "Google\n",
      "JD\n",
      "Taobao\n"
     ]
    }
   ],
   "source": [
    "for site in sites:\n",
    "    print(site)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "52bd7e86-675a-46ed-8b37-7c6a3d3e740c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B\n",
      "a\n",
      "i\n",
      "d\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "for i in sites[0]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b649c150-f0d2-41e9-8241-e0c27b4f98cf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for number in range(1, 6):\n",
    "    print(number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "43fec741-b36c-45f8-9631-330b5d5f263a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'code' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[18], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m6\u001b[39m):  \u001b[38;5;66;03m#len\u001b[39;00m\n\u001b[0;32m      2\u001b[0m     \u001b[38;5;28mprint\u001b[39m(x)\n\u001b[1;32m----> 3\u001b[0m     code\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m      5\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFinally finished!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'code' is not defined"
     ]
    }
   ],
   "source": [
    "for x in range(6):  #len\n",
    "    print(x)\n",
    "    code\n",
    "else:\n",
    "    print(\"Finally finished!\")   # 执行完for 后执行   重点：执行完"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bc6ee8a0-4328-45d3-832a-97f43f8d9624",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Baidu\n",
      "Google\n",
      "Taobao\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(sites)):\n",
    "    if i !=2:\n",
    "        print(sites[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c91be8f2-b571-4044-a50d-050b5de01a9b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8580c7f2-53eb-470e-bd32-59179a2b9da3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
